package com.pktechan.api.service.impl;

import com.pktechan.api.base.BaseMapper;
import com.pktechan.api.base.BaseServiceImpl;
import com.pktechan.api.dao.ShopCartMapper;
import com.pktechan.api.entity.ShopCart;
import com.pktechan.api.service.ShopCartService;
import com.pktechan.api.utils.FastDfsHttpUtil;
import com.pktechan.api.vo.ShopCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("shopCartService")
@Transactional
public class ShopCartServiceImpl extends BaseServiceImpl<ShopCart> implements ShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<ShopCart> getBaseMapper() {
        return shopCartMapper;
    }

    /**
     * 根据购物车id集合查询
     * @param shopCartIdList
     * @return
     */
    @Override
    public List<ShopCartVO> getShopCartByIds(List<Integer> shopCartIdList) {
        return shopCartMapper.getShopCartByIds(shopCartIdList);
    }

    /**
     * 获取购物车列表
     * @param shopCart
     * @return
     */
    @Override
    public List<ShopCartVO> getShopCartList(ShopCart shopCart) {
        List<ShopCartVO> shopCartList = shopCartMapper.getShopCartList(shopCart);
        shopCartList.forEach(shopCartVO -> {
            shopCartVO.setThumbImagePath(FastDfsHttpUtil.getFileHttpUrl(shopCartVO.getThumbImagePath()));
        });
        return shopCartList;
    }

    /**
     * 新增或修改
     * @param shopCart
     */
    @Override
    public void saveOrUpdate(ShopCart shopCart) {
        //根据产品Id与custId查询当前产品是否存在购物车
        ShopCart cart = shopCartMapper.getShopCartByProductIdAndCustId(shopCart);
        //购物车不为空,增加产品加购数量,否则新增数据
        if (cart != null){
            Integer count = 0;
            count = cart.getCount()+shopCart.getCount();
            cart.setCount(count);
            shopCartMapper.update(cart);
        }else{
            shopCartMapper.save(shopCart);
        }
    }
}
